package com.grape.dto;

import com.grape.req.StatisticYearMonthReq;
import com.grape.utils.BigDecimalUtil;
import com.grape.utils.DateUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

@Data
public class StatisticalMonthDTO {

    /** 年份 */
    @ApiModelProperty(value = "年份")
    private String year;

    /** 月份 */
    @ApiModelProperty(value = "月份")
    private String month;

    /** 类型（income: 收入，pay: 支出） */
    @ApiModelProperty(value = "类型（income: 收入，pay: 支出）")
    private String type;

    /** 总笔数 */
    @ApiModelProperty(value = "总笔数")
    private Integer totalNumber;

    /** 总金额（收入，支出） */
    @ApiModelProperty(value = "总金额（收入，支出）")
    private BigDecimal totalMoney;

    /** 日均 */
    @ApiModelProperty(value = "日均")
    private Double averagePerDay;

    /** 分类饼图 */
    @ApiModelProperty(value = "分类饼图")
    private List<CategoryRatioDTO> categoryRatioDTOList;

    /** 分类金额明细 */
    @ApiModelProperty(value = "分类总金额明细")
    private List<CategoryMoneyDTO> categoryMoneyDTOList;

    public static StatisticalMonthDTO createByRequest(StatisticYearMonthReq statisticYearMonthReq) {
        StatisticalMonthDTO statisticalMonthDTO = new StatisticalMonthDTO();
        statisticalMonthDTO.setYear(statisticYearMonthReq.getYear().toString());
        statisticalMonthDTO.setMonth(statisticYearMonthReq.getMonth().toString());
        statisticalMonthDTO.setType(statisticYearMonthReq.getType());
        return statisticalMonthDTO;
    }

    public void initAveragePerDay() {
        if (totalMoney == null || totalNumber == null) {
            this.averagePerDay = 0.0;
            return;
        }
        this.averagePerDay = BigDecimalUtil.getDoubleValue(BigDecimalUtil.div(totalMoney, BigDecimal.valueOf(DateUtils.getDaysByMonth(Integer.parseInt(year), Integer.parseInt(month)))));
    }
}
